package junit.base;

import io.jboot.db.model.JbootModel;
import io.jboot.test.MockMvc;
import io.jboot.test.junit4.JbootRunner;
import io.jboot.utils.StrUtil;
import org.junit.runner.RunWith;

import java.util.HashMap;
import java.util.Map;

@RunWith(JbootRunner.class)
public class BaseTester {

    protected static Map<String, Object> container = new HashMap<>();
    protected static MockMvc mvc = new MockMvc(true);

    protected static Map<String, Object> buildParas(String... keyvalues) {
        Map<String, Object> ret = new HashMap<>();
        for (String keyvalue : keyvalues) {
            String[] strings = keyvalue.split(":");
            ret.put(strings[0], strings[1]);
        }

        return ret;
    }


    protected static Map<String, Object> buildParas(JbootModel<?> model, String prefix) {
        Map<String, Object> ret = new HashMap<>();
        for (String attr : model._getAttrNames()) {
            if (StrUtil.isNotBlank(prefix)) {
                ret.put(prefix + "." + attr, model.get(attr));
            } else {
                ret.put(attr, model.get(attr));
            }

        }
        return ret;
    }

    public static void putContainerData(String key, Object value) {
        container.put(key, value);
    }

    public static <T> T getContainerData(String key) {
        return (T) container.get(key);
    }
}
